// components/my-login2/my-login2.js
var common = require("../../utils/public.js") //引入public.js脚本
const config = require(`../../config.js`);
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    //城市
    city: {
      type: toString
    },
    //微信头像
    avatarUrl: {
      type: toString
    },
    //省份
    province: {
      type: toString
    },
    //性别
    gender: {
      type: toString
    },
    //微信名字
    nickName: {
      type: toString
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    //城市
    city:'',
    //微信头像
    avatarUrl:'',
    //省份
    province:'',
    //性别
    gender:'',
    //微信名字
    nickName:'',
    //用户id
    referee:'',
    //城市id
    cityId:''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 获取手机号
    getPhoneNumber: common.throttleFunc(function (e) {
      console.log(e)
      let that = this

      //允许
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
        let code = ""
        wx.login({
          success: res => {
            wx.request({
              url: `${config.url}/admin/user/login`,
              data: {
                wxCode: res.code,
                city: wx.getStorageSync("userMessage").city == "" ? '-1' : wx.getStorageSync("userMessage").city,
                province: wx.getStorageSync("userMessage").province == "" ? '-1' : wx.getStorageSync("userMessage").province,
                imgurl: wx.getStorageSync("userMessage").avatarUrl,
                loginType: '1',
                sex: wx.getStorageSync("userMessage").gender,
                nickname: wx.getStorageSync("userMessage").nickName,
                type: '-1',
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                referee: that.data.referee == 'undefined' ? '' : that.data.referee,  // 注册人id

                cityId: that.data.cityId == 'undefined' ? '' : that.data.cityId, //城市id
                shopId: '-1'
              },
              header: {
                'content-type': 'application/json',
              },
              method: 'POST',
              success: function (res) {
                console.log(res)
                if (res.data.result == true) {
                  if (wx.getStorageSync("userId") != "") {
                    that.empower()
                  }

                  //0是别人登陆 1是商家登陆 2个人登陆
                  wx.setStorageSync("type", res.data.object.type)
                  //本地存储
                  wx.setStorageSync("token", res.data.data)
                  wx.setStorageSync("phone", res.data.object.phone)
                  //openId
                  wx.setStorageSync("openId", res.data.body.openid)
                  //城市Id
                  //codeNum
                  wx.setStorageSync("codeNum", res.data.code)
                  wx.setStorageSync("userMess", res.data.object)
                  // wx.setStorageSync("balance", res.data.object.balance)

                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 1500,
                    success: () => {
                      wx.reLaunch({
                        url: '/pages/index/index',
                      })
                    }
                  })
                } else if (res.data.result == false) {
                  //codeNum
                  wx.setStorageSync("codeNum", res.data.code)
                  wx.setStorageSync("phone", res.data.object.phone)
                  //本地存储
                  wx.setStorageSync("token", res.data.data)

                  wx.setStorageSync("userMess", res.data.object)
                  //openId
                  wx.setStorageSync("openId", res.data.body.openid)

                  if (res.data.code == 103) {
                    wx.navigateTo({
                      url: '/pages/sf_xz/sf_xz',
                    })
                  } else {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                }

              }
            })
          }
        })


        // wx.getStorage('loginFlag', true)
      } else {

      }

    }),

    //手机验证码登陆
    tapLogin: function () {

      wx.navigateTo({
        url: '../loginPhone/loginPhone?city=' + this.data.city + '&province=' + this.data.province + '&avatarUrl=' + this.data.avatarUrl + '&gender=' + this.data.gender + '&nickName=' + this.data.nickName + '&referee=' + this.data.referee + '&cityId=' + this.data.cityId,
      })
    },

    //登陆
    loginPhone: function (code) {
      let that = this


    },
    empower: function (res) {
      api.empower({
        grantUserId: wx.getStorageSync("userId"),
        userId: wx.getStorageSync("userMess").id,
      }).then(res => {
        console.log(res)
      })
    }


  }
})
